package com.haixiaoke.saas.manage.messageCenter.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.messageCenter.domain.PushCondition;
import com.haixiaoke.saas.messageCenter.service.IPushConditionService;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 推送条件信息Controller
 *
 * @author xiaolong
 * @date 2024-11-11
 */
@RestController
@RequestMapping("/manage/messageCenter/pushCondition")
public class ManagePushConditionController extends BaseController {
    @Autowired
    private IPushConditionService pushConditionService;

    /**
     * 查询推送条件信息列表
     */
    @PreAuthorize("@ss.hasPermi('manage:pushCondition:list')")
    @GetMapping("/list")
    public TableDataInfo list(PushCondition pushCondition) {
        startPage();
        List<PushCondition> list = pushConditionService.selectPushConditionList(pushCondition);
        return getDataTable(list);
    }

    /**
     * 查询机构下推送条件信息
     */
    @GetMapping("/info")
    public AjaxResult info(PushCondition pushCondition) {
        if (StrUtil.isEmpty(pushCondition.getTenantId())) {
            return error("请选择机构");
        }
        if (StrUtil.isEmpty(pushCondition.getMessageType())) {
            return error("请选择消息类型");
        }
        List<PushCondition> list = pushConditionService.selectPushConditionList(pushCondition);
        return success(list);
    }


    /**
     * 导出推送条件信息列表
     */
    @PreAuthorize("@ss.hasPermi('manage:pushCondition:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, PushCondition pushCondition) {
        List<PushCondition> list = pushConditionService.selectPushConditionList(pushCondition);
        ExcelUtil<PushCondition> util = new ExcelUtil<PushCondition>(PushCondition.class);
        util.exportExcel(response, list, "推送条件信息数据");
    }

    /**
     * 获取推送条件信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:pushCondition:query')")
    @GetMapping(value = "/{pushConditionId}")
    public AjaxResult getInfo(@PathVariable("pushConditionId") Long pushConditionId) {
        return success(pushConditionService.selectPushConditionByPushConditionId(pushConditionId));
    }

    /**
     * 新增推送条件信息
     */
    @PreAuthorize("@ss.hasPermi('manage:pushCondition:add')")
    @PostMapping
    public AjaxResult add(@RequestBody @Valid PushCondition pushCondition) {
        pushCondition.setCreateBy(getUsername());
        return toAjax(pushConditionService.insertPushCondition(pushCondition));
    }

    /**
     * 修改推送条件信息
     */
    @PreAuthorize("@ss.hasPermi('manage:pushCondition:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody PushCondition pushCondition) {
        pushCondition.setUpdateBy(getUsername());
        return toAjax(pushConditionService.updatePushCondition(pushCondition));
    }

    /**
     * 删除推送条件信息
     */
    @PreAuthorize("@ss.hasPermi('manage:pushCondition:remove')")
	@DeleteMapping("/{pushConditionIds}")
    public AjaxResult remove(@PathVariable Long[] pushConditionIds) {
        return toAjax(pushConditionService.deletePushConditionByPushConditionIds(pushConditionIds));
    }
}
